package com.yunfeisoft.business.dao.impl;

import com.yunfeisoft.business.dao.inter.OutHospitalDao;
import com.yunfeisoft.business.model.Elder;
import com.yunfeisoft.business.model.OutHospital;
import com.yunfeisoft.business.model.OutHospitalApprovalResult;
import com.yunfeisoft.common.base.BaseDaoImpl;
import com.yunfeisoft.common.sql.builder.SelectBuilder;
import com.yunfeisoft.common.sql.builder.WhereBuilder;
import com.yunfeisoft.common.utils.Page;
import org.springframework.stereotype.Repository;
import org.springframework.util.ObjectUtils;

import java.util.Map;

/**
 * ClassName: OutHospitalDaoImpl
 * Description: 老人出院流转表Dao实现
 * Author: Jackie liu
 * Date: 2020-01-13
 */
@Repository
public class OutHospitalDaoImpl extends BaseDaoImpl<OutHospital, String> implements OutHospitalDao {

    @Override
    public Page<OutHospital> queryPage(Map<String, Object> params) {
        WhereBuilder wb = new WhereBuilder();
        wb.setOrderByWithDesc("createTime");
        if (params != null) {
            initPageParam(wb, params);
            wb.andEquals("e.orgId", params.get("orgId"));
            wb.andFullLike("e.name", params.get("elderName"));
            if (!ObjectUtils.isEmpty(params.get("status"))) {
                wb.andEquals("o.status", params.get("status"));
            }
        }
        SelectBuilder builder = getSelectBuilder("o");
        builder.column("e.name as elderName")
                .column("e.sex as sex")
                .column("e.birthDate as birthDate")
                .column("e.idcard as idcard")
                .column("e.code as code")
                .column("e.bedNum as bedNum")
                .column("e.checkInDate as checkInDate")
                .column("e.nursingLevel as nursingLevel")
                .join(Elder.class)
                .alias("e")
                .on("e.id = o.elderId").build();
        return queryPage(builder.getSql(), wb);
    }

    @Override
    public Page<OutHospital> myApprovalList(Map<String, Object> params) {
        WhereBuilder wb = new WhereBuilder();
        wb.setOrderByWithDesc("a.createTime");
        if (params != null) {
            initPageParam(wb, params);
            wb.andFullLike("e.name", params.get("elderName"));
            wb.andFullLike("a.createId", params.get("createId"));
            if (!ObjectUtils.isEmpty(params.get("status"))) {
                wb.andEquals("o.status", params.get("status"));
            }
        }
        SelectBuilder builder = getSelectBuilder("o");
        builder.column("e.name as elderName")
                .column("e.sex as sex")
                .column("e.birthDate as birthDate")
                .column("e.idcard as idcard")
                .column("e.code as code")
                .column("e.bedNum as bedNum")
                .column("e.checkInDate as checkInDate")
                .column("e.nursingLevel as nursingLevel")
                .join(Elder.class)
                .alias("e")
                .on("e.id = o.elderId").build();
        builder.column("a.status as approvalStatus")
                .column("a.createTime as approvalTime")
                .join(OutHospitalApprovalResult.class).alias("a")
                .on("a.outHospitalId = o.id").build();
        return queryPage(builder.getSql(), wb);
    }
}